/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                       (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License (version 2) as published
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with
    Amrita ITEWS if not then it can be viewed here:
    http://itews.amrita.ac.in/license.html.


    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/


    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews


    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa
    Vidyapeetham as part of the project titled,"Intelligent & Interactive
    Telematics using Emerging Wireless Technologies for Transport Systems
    (ITEWS)" sponsored by Technology Information Forecasting and Assessment
    Council (TIFAC), India.
***/

/*! \file stream_eventq.h
    \brief Stream Event Queue Header File.
    */

#ifndef STREAM_EVENTQ_H_
#define STREAM_EVENTQ_H_
#include <eventq.h>



/*! \brief Create Server Socket (BIND) Event Type Flag,
  PUT event,
  param (I) - address of stream_event_hdr which contains port to bind,
  buf - none.
 */
#define     EVT_STREAM_BIND                             0x00

/*! \brief Server Socket Send Data Event Type Flag,
  PUT event,
  param (I) - if present is treated as connected fd to which
  the data has to be send, otherwise the fd stored in the context
  is used,
  buf (I) - data to be send.
 */
#define     EVT_STREAM_PUT_DATA                         0x01

/*! \brief Server Socket Receive Data Event Type Flag,
  GET event,
  param (I) - if present is treated as connected fd to which
  the data has to be received otherwise the fd stored in the context is
  used,
  buf (O) - buffer to which data has to be read
  (must be allocated by the caller).
 */
#define     EVT_STREAM_GET_DATA                         0x02


/*! \brief Server Socket Get Connection (Accept) Event Type Flag.
 GET event,
 param (O) - address of stream_event_hdr to which remote IP,
   port, and connected fd is returned,
 buf - none.
*/
#define     EVT_STREAM_ACCEPT                           0x03

/*! \brief Client Socket Connect to Server Event Type Flag
 PUT event,
 param (I) - address of stream_event_hdr which contains remote IP/port,
 buf - none.
 */
#define     EVT_STREAM_CONNECT                          0x04

/*! \brief Server Socket Listen for Connection Event Type Flag,
 PUT event,
 param - none,
 buf - none.
 */
#define     EVT_STREAM_LISTEN                           0x05

/*! \brief Select Connection Event Type Flag,
PUT event,
param (I) - address of stream_opt_t which holds option, and fd to be operated
buf - if option is STREAM_OPT_ADDR_REUSE - none
      if option is STREAM_OPT_READ_TIMEOUT - address of int32 variable which
      holds the timeout value in milliseconds, -1 means infinite.
*/
#define     EVT_STREAM_SET_OPT                          0x06

/*! \brief Enables local address reuse . */
#define     STREAM_OPT_ADDR_REUSE                       1

/*! \brief Set Stream Socket Read Timeout
   while setting this option, timeout in milliseconds has to
   mention.
 */
#define     STREAM_OPT_READ_TIMEOUT                     2


/*! \brief Close Client Connection Event Type Flag
PUT event,
param (I) - if present is treated as connected sock fd, otherwise
the sock fd in the context is closed( In this case eventq_close() should be called explicitly by the caller),
buf - none.
*/
#define     EVT_STREAM_CLOSE                            0x07

/*! \brief Maximum Size in Bytes of IP Address String.*/
#define     STREAM_MAX_IPSTR_SZ                         16

/*! \brief Maximum connections that can listen by the stream socket.*/
#define     STREAM_LISTEN_BACKLOG                       5

/*! \brief Enables recording of debugging information. */
#define     STREAM_OPT_DEBUG                            3

/*! \brief Enables duplicate address and port binding. */
#define     STREAM_OPT_REUSE_PORT                       4

/*! \brief Enables keep connection alive. */
#define     STREAM_OPT_KEEPALIVE                        5

/*! \brief Enables rooting bypass for outgoing messages. */
#define     STREAM_OPT_DONTROUTE                        6

/*! \brief Enables permission to tramsmit broadcast messages. */
#define     STREAM_OPT_BROADCAST                        7

/*! \brief Linger on close if data present. */
#define     STREAM_OPT_LINGER                           8

/*! \brief Enables reception of out-of-band data in band. */
#define     STREAM_OPT_OUT_OF_BIND_DATA_INLINE          9

/*! \brief Set buffer size for output. */
#define     STREAM_OPT_SET_SNDBUF                       10

/*! \brief Set buffer size for input. */
#define     STREAM_OPT_SET_RCVBUF                       11

/*! \brief Set minimum count for output. */
#define     STREAM_OPT_SET_MIN_CNT_OUTPUT               12

/*! \brief Set minimum count for input. */
#define     STREAM_OPT_SET_MIN_CNT_INPUT                13

/*! \brief  Set timeout value for output. */
#define     STREAM_OPT_SEND_TIMEOUT                     14

/*! \brief Do not generate SIGPIPE, instead return EPIPE . */
#define     STREAM_OPT_NOSIGPIPE                        15

/*! \brief Event Queue Operation Structure Global Variable.*/
extern eventq_ops_t stream_eventq_ops;

/*! \brief Stream EventQ Event Param Structure.*/
typedef struct
{
    /*! \brief IP Address for event Manipulation.*/
    int8    ip[ STREAM_MAX_IPSTR_SZ ];

    /*! \brief Port Number for event Manipulation.*/
    uint16  port;

    /*! \brief Connected fd.*/
    int32   conn_fd;
}stream_param_t;

/*! \brief Stream EventQ Options Structure.*/
typedef struct
{
    /*! \brief Stream Socket Option to be operated.*/
    int32   sock_opt;

    /*! \brief Stream Sock fd on which the operation has to do.*/
    int32   sock_fd;
}stream_opt_t;

/*SPI*/
STATUS stream_eventq_open       ( eventq_ctx_t *e_ctx, const char *eventq_name );
STATUS stream_eventq_close      ( eventq_ctx_t *e_ctx );

STATUS stream_eventq_sync_put   ( eventq_ctx_t *e_ctx, event_t *e, HRESULT *res );
STATUS stream_eventq_sync_get   ( eventq_ctx_t *e_ctx, event_t *e );

#endif /*STREAM_EVENTQ_H_*/

